<?php
namespace app\admin\controller;
use app\admin\logic\AdvLogic;

/**
 * 广告管理api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2020/04/26
 * Time: 10:18
 */
class Adv extends Base {

    public function __construct(){
        parent::__construct();
        $this->a_logic = new AdvLogic();
    }

    /**
     * 获取广告详情
     * @return \think\response\Json
     */
    public function ajax_get_adv_detail(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = [];
        $maps[] = ['id','=',$params['advid']];

        $a_detail = model('adv')->get_detail($maps,'id,pic');
        $a_detail['pic'] = !empty($a_detail['pic']) ? json_decode($a_detail['pic'],true) : [];

        $data['detail'] = $a_detail;
        return json($this->a_logic->get_response(1,'OK',$data));
    }

    /**
     * 修改广告
     * @return \think\response\Json
     */
    public function ajax_save_adv(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查修改广告规则
        $r = $this->a_logic->check_save_adv_rule($params);
        if(!$r['code']){
            return json($r);
        }
        $a_detail = $r['data']['a_detail'];

        $a_data = [];
        $a_data['pic'] = json_encode($params['pic'],JSON_UNESCAPED_UNICODE);

        $maps = [];
        $maps[] = ['id','=',$a_detail['id']];
        $r = model('adv')->force()->save($a_data,$maps);
        if($r !== false){
            return json($this->a_logic->get_response(1,'修改成功'));
        }
        return json($this->a_logic->get_response(0,'修改失败'));
    }
}